#include "stdafx.h"
#include "CloudEffect.h"

Interface_SplashScreen_Effect_Cloud :: Interface_SplashScreen_Effect_Cloud()
:mTimepassed(0.0f)
{
}

Interface_SplashScreen_Effect_Cloud :: ~Interface_SplashScreen_Effect_Cloud()
{
	mCloudImg.release();

	mHandler.reset(NULL);
	mHandler.release();
}

bool Interface_SplashScreen_Effect_Cloud :: Initialize()
{
	for(int i = 0; i < 4; i++)
		for(int j = 0; j < 2; j++)
			mSpawnPoints.push_back(Vector2(-APP_WIDTH * 0.8f + j * 150.0f, -APP_HEIGHT * 0.4f + i * 100.0f));

	mHandler = SmartPtr<ParticleSystem>(new ParticleSystem(20));

	mCloudImg = SmartPtr<hgeSprite>(g_pSplashScreen->RESOURCE()->GetSprite("SplashScreen_EffectCloud"));
	mCloudImg->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
	return true;
}

void Interface_SplashScreen_Effect_Cloud :: Update(const float & Interval)
{
	mTimepassed += Interval;
	if(mTimepassed > 0.24f && mHandler->ActiveCount() < 20)
	{
		mHandler->Emit(1, mSpawnPoints[rand() % mSpawnPoints.size()]);
		mTimepassed = 0.0f;
	}
	mHandler->Update(Interval);
}

void Interface_SplashScreen_Effect_Cloud :: Render()
{
	g_pSplashScreen->HGESYSTEM()->System_SetState(HGE_TEXTUREFILTER , false);
	std :: deque <Graphic_Particle> * ReferenceList = mHandler->GetParticles();
	Graphic_Particle * Ref = NULL;

	float	Width = mCloudImg->GetWidth(), Height = mCloudImg->GetHeight();
	for(int i = static_cast<int>(ReferenceList->size()) - 1; i != -1; i--)
	{
		Ref = &ReferenceList->at(i);
		if(Ref->mRespawn || Ref->mLife <= 0.0f) continue;
		mCloudImg->SetTextureRect(	static_cast<int>(Ref->mScaleDelta.y) * Width, 0, 
									Width, Height, false);

		mCloudImg->SetColor(Ref->mColor);
		mCloudImg->RenderEx(Ref->mPosition.x, Ref->mPosition.y, 0.7855f, Ref->mScale.x, Ref->mScale.y);
	}
	g_pSplashScreen->HGESYSTEM()->System_SetState(HGE_TEXTUREFILTER , true);
	Ref = NULL;
	ReferenceList = NULL;
}